package com.imooc.portal.mq;

import lombok.Setter;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Component
public class RocketMqUpDimensionProducer implements ApplicationContextAware, EnvironmentAware {

    private static ApplicationContext applicationContext;
    private static Environment environment;

    /**
     * 发送维度升级消息
     * @param message
     * @param callback
     */
    public static void sendMsg(Object message, SendCallback callback) {
        RocketMQTemplate rocketMQTemplate = applicationContext.getBean(RocketMQTemplate.class);
        String upDimensionTopic = environment.getProperty("topic.data-center.updimension");
        rocketMQTemplate.asyncSend(upDimensionTopic, message, callback);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
